<template>
    <el-container class="home-container">
      <!-- 头部区域 -->
      <el-header>
        <div>
          <img src="../assets/images/hai.jpg" alt="">
          <span>电商后台管理系统</span>
        </div>
         <button class="btn" @click="logout">退出</button></el-header>
      <!-- 主体区域 -->
      <el-container>
        <!-- 侧边栏区域 -->
        <el-aside :width="flag? '64px' : '200px'">
          <!-- 点击此按钮，将侧边栏进行隐藏 -->
          <div class="toggle_button" @click="toggleCollapse">|||</div>
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409EFF" 
            unique-opened :collapse='flag' 
            :collapse-transition='false'
            router
            :default-active="activePath">
            <!-- unique-opened、collapse这些方法都是element-ui自带的 -->
            <!-- 通过elemnt-ui自带的router，我们就不需要一个个去添加路由了以及点击事件 -->
            <!-- 一级菜单 -->
            <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
              <template slot="title">
                <i :class="iconsObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <!-- 这里不用纠结，是访问地址返回为我们的path -->
                <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" 
                :key="subItem.id" @click="saveNavState('/' + subItem.path)">
                  <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{subItem.authName}}</span>
                </template>
                </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧区域 -->
        <el-main>
          <!-- 如果是路由，可以用占位符就行了，完全没必要使用import引入再渲染,可以看APP组件的使用就知道了 -->
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
    
</template>
<script>


  export default {
    name: 'home',
    data () {
      return {
        menuList: [],
        iconsObj: {//这里是每个左侧边栏对应的图标ID，根据它去动态改变图标
          125: 'el-icon-s-opportunity',
          103: 'el-icon-s-comment',
          101: 'el-icon-camera-solid',
          102: 'el-icon-upload',
          145: 'el-icon-message-solid'
        },
        flag: true,
        activePath: ''
      }
    },
    created() {
      this.getMenuList(),
      this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods: {
      logout() {
        //退出的话，会清除缓存，以及返回登录界面
        window.sessionStorage.clear()
        this.$router.push('/login')
      },
      //获取所有菜单
      async getMenuList() {
        const {data: res} = await this.$http.get('menus')
        //判断是否获取成功，如果成功，则将数据赋值给data里面的menuList
        this.menuList = res
      },
      toggleCollapse() {
        //点击按钮切换菜单的折叠与展开
        this.flag = !this.flag
      },
      saveNavState(activePath) {
        //把数据绑定到sessionstorage，以便于与点击谁，谁就高亮显示
        window.sessionStorage.setItem('activePath', activePath)
      }
    }
  }
</script>
<style  scoped>
  .home-header {
    width: 100%;
    height: 100%;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    color: white;
    background-color: #373d41;
    align-items: center;
  }
  .el-aside {
    background-color: #333744;
  }
  .el-main {
    background-color: #eaedf1;
  }
  .toggle_button {
    background-color: #4A5064;
    color: white;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>

